package com.woniu.uums.controller;

import com.woniu.uums.pojo.ZfWhite;
import com.woniu.uums.service.ZfWhiteService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.woniu.common.constant.FinanceConstant.RedisKey.WHITE;

/**
 * @program: finance021
 * @description: 白名单管理
 * @create: 2024-11-09 10:49
 **/
@RestController
public class WhiteController {
    @Autowired
    private ZfWhiteService zfWhiteService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 初始化白名单管理
     *
     * @return
     */
    @RequestMapping("/init")
    public String init() {
        List<ZfWhite> list = zfWhiteService.list();
        for (ZfWhite white : list) {
            stringRedisTemplate.opsForHash().put(WHITE, white.getPath(), white.getRouteType());
        }
        return "初始化白名单成功";
    }

    /**
     * 添加白名单
     *
     * @param financeWhite
     * @return
     */
    @SneakyThrows
    @PostMapping("/addWhite")
    public String addWhite(@RequestBody ZfWhite financeWhite) {
        //删除redis数据
        stringRedisTemplate.opsForHash().delete(WHITE, financeWhite.getPath());
        //向数据库插入数据
        boolean save = zfWhiteService.save(financeWhite);

        Thread.sleep(100);

        //向redis中添加数据
        stringRedisTemplate.opsForHash().put(WHITE, financeWhite.getPath(), financeWhite.getRouteType());
        if (save) {
            return "添加白名单成功";
        }
        return "添加白名单失败";
    }

    /**
     * 删除白名单
     *
     * @param
     * @return
     */
    @SneakyThrows
    @PostMapping("/delWhite")
    public String delWhite(@RequestParam("id") Long id) {
        ZfWhite byId = zfWhiteService.getById(id);
        //删除redis数据
        stringRedisTemplate.opsForHash().delete(WHITE, byId.getPath());
        //向数据库del数据
        boolean del = zfWhiteService.removeById(id);

        Thread.sleep(100);

        //向redis中del数据
        stringRedisTemplate.opsForHash().delete(WHITE, byId.getPath());
        if (del) {
            return "del白名单成功";
        }
        return "del白名单失败";
    }

    /**
     * 修改白名单
     * @param
     * @return
     */
    @SneakyThrows
    @PostMapping("/updateWhite")
    public String updateWhite(@RequestBody ZfWhite financeWhite) {
        ZfWhite byId = zfWhiteService.getById(financeWhite.getId());//byId是老数据
        stringRedisTemplate.opsForHash().delete(WHITE, byId.getPath());

        //向数据库update数据
        boolean update = zfWhiteService.updateById(financeWhite);

        Thread.sleep(100);

        //删除redis数据
        stringRedisTemplate.opsForHash().delete(WHITE, byId.getPath());//byId是老数据,延迟双删
        stringRedisTemplate.opsForHash().delete(WHITE, financeWhite.getPath());

        //向redis中update数据
        stringRedisTemplate.opsForHash().put(WHITE, financeWhite.getPath(), financeWhite.getRouteType());
        if (update) {
            return "update白名单成功";
        }
        return "update白名单失败";
    }
}